<?php
namespace app\index\controller;


use app\common\model\Visitor;
use app\common\model\Visitordata;

class CountVisitor
{
    /**
     * 获得访客操作系统
     */
    public function get_os() {
        if (!empty($_SERVER['HTTP_USER_AGENT'])) {
            $os = $_SERVER['HTTP_USER_AGENT'];
            if (preg_match('/win/i', $os)) {
                $os = 'Windows';
            } else if (preg_match('/mac/i', $os)) {
                $os = 'MAC';
            } else if (preg_match('/linux/i', $os)) {
                $os = 'Linux';
            } else if (preg_match('/unix/i', $os)) {
                $os = 'Unix';
            } else if (preg_match('/bsd/i', $os)) {
                $os = 'BSD';
            } else {
                $os = 'Other';
            }
            return $os;
        } else {
            return 'unknow';
        }
    }

    /**
     * 获得访问者浏览器
     */
    public function browse_info() {
        if (!empty($_SERVER['HTTP_USER_AGENT'])) {
            $br = $_SERVER['HTTP_USER_AGENT'];
            if (preg_match('/MSIE/i', $br)) {
                $br = 'MSIE';
            } else if (preg_match('/Firefox/i', $br)) {
                $br = 'Firefox';
            } else if (preg_match('/Chrome/i', $br)) {
                $br = 'Chrome';
            } else if (preg_match('/Safari/i', $br)) {
                $br = 'Safari';
            } else if (preg_match('/Opera/i', $br)) {
                $br = 'Opera';
            } else {
                $br = 'Other';
            }
            return $br;
        } else {
            return 'unknow';
        }
    }

    /**
     * 获得访问者浏览器语言
     */
    public function get_lang() {
        if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
            $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
            $lang = substr($lang, 0, 5);
            if (preg_match('/zh-cn/i',$lang)) {
                $lang = '简体中文';
            } else if (preg_match('/zh/i',$lang)) {
                $lang = '繁体中文';
            } else {
                $lang = 'English';
            }
            return $lang;
        } else {
            return 'unknow';
        }
    }
// 获取IP地址
    public function get_client_ip($type = 0) {
        $type    = $type ? 1 : 0;
        static $ip =  NULL;
        if ($ip !== NULL) return $ip[$type];
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr  =  explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos  =  array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip   =  trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip   =  $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip   =  $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = ip2long($ip);
        $ip  = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }
// 数据库操作
    public function visitor(){
        $visitor = new Visitor();
        $visitor_data = new Visitordata();

        $count_visitor = new CountVisitor();
        $ip=$count_visitor->get_client_ip();
        $system = $count_visitor->get_os();
        $browser = $count_visitor->browse_info();
        $result = $visitor->where('v_ip',$ip)->find();
        $time = date("Y-m-d-H:i");
        $time_a = date("H:i:s");
        if (empty($result)) {
            $data = [
                [   'v_ip' => $ip,
                    'v_system' => $system,
                    'v_browser' => $browser,
                    'v_time' => $time ,
                    'v_record' => $time,
                    'v_time_a' => $time_a
                ],
            ];
            $DATAresult1 = $visitor->insertAll($data);


            $number1 = $visitor_data->where('id', "1")->value("number_all");
            $number1++;
            $visitor_data->where('id', "1")->update(['number_all' => $number1]);

            if(!empty($DATAresult1)){
                $res['msg'] = "001";
                return $res['err'] = "新增加了一个访客";
            }
        }else{
            $number = $visitor->where('v_ip',$ip)->value("v_number");
            $rowTime = $visitor->where('v_ip',$ip)->value("v_time");
            if (!empty($number) && $rowTime != $time) {
                $time = date("Y-m-d-H:i");
                $time_a = date("H:i:s");
                $number++;
                $DATAresult2 = $visitor->where('v_ip', $ip)->update(['v_number' => $number]);
                $DATAresult3 = $visitor->where('v_ip', $ip)->update(['v_record' => $time]);
                $DATAresult4 = $visitor->where('v_ip', $ip)->update(['v_browser' => $browser]);
                $DATAresult4 = $visitor->where('v_ip', $ip)->update(['v_time_a' => $time_a]);

                $number2 = $visitor_data->where('id', "1")->value("number_all");
                $number2++;
                $visitor_data->where('id', "1")->update(['number_all' => $number2]);                $res['msg'] = "002";

                return $res['err'] = "旧访客访问次数加1";
            }
        }
        return "增加失败";
    }
}